package com.company.psm.util;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.company.psm.entity.User;
import com.company.psm.service.UserManagement;
@Controller
public class Util {
	public static Long ADMIN_STORE_ID = new Long(3);
	
	@Autowired
	private static UserManagement userManagement;
    public static String encryptMD5(String input) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] messageDigest = md.digest(input.getBytes());
            BigInteger number = new BigInteger(1, messageDigest);
            String hashtext = number.toString(16);
            while (hashtext.length() < 32) {
                hashtext = "0" + hashtext;
            }
            return hashtext;
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }
    public static User getCurrentUser(){
    	Authentication authentication = SecurityContextHolder.getContext().
      		  getAuthentication();
      		  String name = authentication.getName();
      		  System.err.println("NAME = " + name);
      	return userManagement.getUserByAccount(name);
    }
    public static Long getCurrentStoreId(){
    	return getCurrentUser().getStore().getId();
    }
    
    public static List<String> toInGradientName(String str){
    	List<String> listGradient = new ArrayList<String>();
    	String tmp[] = str.split(",");
    	for (String gradient : tmp) {
			if(gradient != null && !gradient.equals("")){
				listGradient.add(gradient);
			}
		}
    	return listGradient;
    }
    public static List<String> toMedicineId(String str){
    	List<String> listMedicine = new ArrayList<String>();
    	String tmp[] = str.split(",");
    	for (String medicineId : tmp) {
			if(medicineId != null && !medicineId.equals("")){
				listMedicine.add(medicineId);
			}
		}
    	return listMedicine;
    }
    public static String dotPrice(String priceString){
		String invertPrice = "";
		String tmp = "";
		String price = "";
		priceString = priceString.replaceAll(",", "");
		if(priceString.length() > 3){
			for(int i = priceString.length(); i > 0 ; i--){
				invertPrice +=priceString.substring(i-1, i);
			}
		}
		for(int i=0; i <invertPrice.length() ; i++){
			tmp+=invertPrice.substring(i, i+1);
			if(i>1 && (i+1)%3 == 0 && (i+1)<invertPrice.length()){
				tmp +=",";
			}
		}

		for(int i = tmp.length(); i > 0 ; i--){
			price +=tmp.substring(i-1, i);
		}
		return price;
    }
    public static Date toDate(String dateString){
    	SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
    	Date date = null;
    	try {
    		date = formatter.parse(dateString);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return date;
    }
    
    public static void main(String[] args) {
    	System.err.println(dotPrice("3,000,000,000"));
	}
}
